package com.itheima.dynamic;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Test01 {

    @Test
    public void selectByIds() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = build.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(3);

        List<Student> list = mapper.selectByIds(ids);

        for (Student student1 : list) {
            System.out.println(student1);
        }

        sqlSession.close();
        is.close();

    }

    @Test
    public void selectCondition() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = build.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = new Student();
//        student.setId(2);
        student.setName("李四");
        student.setAge(24);

        List<Student> list = mapper.selectCondition(student);

        for (Student student1 : list) {
            System.out.println(student1);
        }

        sqlSession.close();
        is.close();

    }
}
